using System;
using System.Collections.Generic;
using System.Text;

namespace Acms.AcmsControls
{
    public class PdfContentControlDisplay : BaseContentControlDisplay<PdfContentControlProperties>
    {
        private System.Web.UI.WebControls.HyperLink link;

        protected override void CreateChildControls()
        {
            link = new System.Web.UI.WebControls.HyperLink();
            this.Controls.Add(link);
            base.CreateChildControls();
        }

        protected override void DataBindChildren()
        {
            if (Properties != null && Properties.HasResource())
            {
                link.NavigateUrl = string.Format("~/File.aspx?Id={0}&Path={1}&Name={2}", Properties.ResourceId.ToString(), Page.Server.UrlEncode(CurrentContext.CurrentPost.Path), Page.Server.UrlEncode(this.ControlName));
                link.ToolTip = Properties.Name;
                if (!string.IsNullOrEmpty(ImageUrl))
                {
                    link.ImageUrl = ImageUrl;
                }
                else
                {
                    link.Text = string.IsNullOrEmpty(Text) ? Properties.Name : Text;
                }

                if (!string.IsNullOrEmpty(Target))
                {
                    link.Target = this.Target;
                }
            }
        }

        public override bool IsEmpty
        {
            get 
            {
                if (Properties != null && Properties.HasResource())
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
        }


        public string ImageUrl { get; set; }

        private string target;
        public string Target
        {
            get { return target; }
            set { target = value; }
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            EnsureChildControls();
            if (this.link == null || string.IsNullOrEmpty(this.link.NavigateUrl))
            {
                this.Visible = false;
            }
        }

        public string Text { get; set; }

        protected override object SaveViewState()
        {
            ViewState["Text"] = Text;
            return base.SaveViewState();
        }

        protected override void LoadViewState(object savedState)
        {
            base.LoadViewState(savedState);
            Text = ViewState["Text"] as string;
        }
    }
}
